package cn.wsalix.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.CommonServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.entity.MenuPermit;
import cn.wsalix.admin.entity.Permit;
import cn.wsalix.admin.form.PermitForm;
import cn.wsalix.admin.repository.MenuPermitRepository;
import cn.wsalix.admin.repository.PermitRepository;
import cn.wsalix.admin.service.MenuService;
import cn.wsalix.admin.service.PermitService;
import cn.wsalix.constant.Global;
import cn.wsalix.exception.ExistException;

@Service("permitService")
public class PermitServiceImpl extends
		CommonServiceImpl<PermitForm, PermitRepository, Permit> implements
		PermitService {
	@Autowired
	private PermitRepository permitRepository;
	@Autowired
	private ExistException existException;
	@Autowired
	private MenuService menuService;
	@Autowired
	private MenuPermitRepository menuPermitRepository;
	@Override
	public String module() {
		return "/admin/permit";
	}

	@Override
	public PermitRepository getRepository() {
		return permitRepository;
	}

	@Override
	public Class<Permit> getEntityClazz() {
		return Permit.class;
	}

	@Override
	public List<MenuPermit> findAllMenuPermit() {
		return menuPermitRepository.findAll();
	}
	@Transactional
	@Override
	public Permit add(PermitForm form) {
		if (isExistByCode(form.getCode())) {
			throw Global.getException("code.exist",
					existException.getCodeExist());
		}
		return super.add(form);
	}

	@Transactional
	@Override
	public Permit edit(PermitForm form) {
		if (isExistByCode(form.getCode())) {
			throw Global.getException("code.exist",
					existException.getCodeExist());
		}
		return super.edit(form);
	}

	private boolean isExistByCode(String code) {
		List<Permit> permits = permitRepository.findByCode(code);
		return !permits.isEmpty() && permits.size() > 0;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(PermitForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getCode() != null && !form.getCode().equals("")) {
			filters.put("code",
					new SearchFilter("code", Operator.EQ, form.getCode()));
		}
		return filters;
	}

	@Override
	public List<Permit> findUnsByMenuId(Long menuId) {
		return permitRepository.findUnsByMenuId(menuId);
	}

	@Transactional
	@Override
	public void delAll() {
		permitRepository.deleteAllInBatch();
	}

	@Override
	public List<Permit> findAdminPermits() {
		return permitRepository.findAll();
	}
	

	@Override
	public Permit findByCodeOne(String code) {
		logger.info(code);
		List<Permit> objs = findsByCode(code);
		if (objs.size() != 1) {
			if (objs.size() > 1) {
				logger.error(code + " : size() > 1 [" + objs.size() + "]");
				return objs.get(0);
			}
			logger.error(code + " : size() =0 ");
			return null;
		} else {
			return objs.get(0);
		}
	}

	@Override
	public Permit findByNameOne(String name) {
		logger.info(name);
		List<Permit> objs = findsByName(name);
		if (objs.size() != 1) {
			if (objs.size() > 1) {
				logger.error(name + " : size() > 1 [" + objs.size() + "]");
				return objs.get(0);
			}
			logger.error(name + " : size() =0 ");
			return null;
		} else {
			return objs.get(0);
		}
	}

	@Override
	public List<Permit> findsByName(String name) {
		return permitRepository.findByName(name);
	}

	@Override
	public List<Permit> findsByCode(String code) {
		return permitRepository.findByCode(code);
	}
}
